package 二叉树及其三种序;

/**
 * @author 冷加宝
 * @version 1.0
 */
// 递归序的解释
// 用递归实现二叉树的三序遍历
public class BinaryTreeTraversalRecursion {
    public static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    // 递归基本样子，用来理解递归序
    public static void f(TreeNode head){
        if(head == null){
            return;
        }
        //1
        f(head.left);
        //2
        f(head.right);
        //3
    }

    // 先序打印所有节点，递归版
    public static void preOrder(TreeNode head){
        if(head == null){
            return;
        }
        System.out.println(head.val + " ");
        preOrder(head.left);
        preOrder(head.right);
    }

    // 中序打印所有节点，递归版
    public static void inOrder(TreeNode head){
        if(head == null){
            return;
        }
        preOrder(head.left);
        System.out.println(head.val + " ");
        preOrder(head.right);
    }

    // 后序打印所有节点，递归版
    public static void posOrder(TreeNode head){
        if(head == null){
            return;
        }
        preOrder(head.left);
        preOrder(head.right);
        System.out.println(head.val + " ");
    }
}
